<?php
namespace Kuoqi\Html\Page;
//关于api的部分的直接输出
class CodeApi{

    public $codes = array();//保存的代码片段

    public $format = 'code-api';//常用类型[code,text]
    public $stream = '';//代码输出流



    public $type;//kuoqiDocument
    public $name;//接口名称
    public $group;//接口分组
    public $title;//接口标题说明，放置到标题部分的说明文档
    public $href;//接口的请求地址信息
    public $method;//接口的请求方式
    public $data;//接口的请求数据内容
    public $ok;//接口成功返回的内容信息
    public $error;//接口失败返回的内容信息
    public $remark;//备注信息，让用户知道的本接口的相关的技术细节
    public $author = '管理员';
    public $date = '';


    /**
     *apitype:kuoqiDocusment
     *apiname:demo参考
     *apidate:2022/01/12
     *apigroup:项目
     *apititle:图片列表接口信息
     *apihref:http://api.kuoqps.com/project/imgs
     *apimethod:request
     *apidata:
    |app_key|string|1|""|项目唯一识别CODE|
     *apiok:接口成功返回的内容信息
     *apierror:接口失败返回的内容信息
     *apiremark:暂无备注信息
     */
    public function __construct($config = array()){
        if(isset($config['type'])){$this->type = $config['type'];}
        if(isset($config['name'])){$this->name = $config['name'];}
        if(isset($config['group'])){$this->group = $config['group'];}
        if(isset($config['title'])){$this->title = $config['title'];}
        if(isset($config['href'])){$this->href = $config['href'];}
        if(isset($config['method'])){$this->method = $config['method'];}
        if(isset($config['data'])){$this->data = $config['data'];}
        if(isset($config['ok'])){$this->ok = $config['ok'];}
        if(isset($config['error'])){$this->error = $config['error'];}
        if(isset($config['remark'])){$this->remark = $config['remark'];}
        if(isset($config['date'])){$this->date = $config['date'];}
    }

    //flushData
    /**
    $parm = explode('|',$param);
    $item = array();
    $item['name'] = 'param';
    $item['paramName'] = isset($parm[0])?$parm[0]:null;//输出参数名称
    $item['type'] = isset($parm[1])?$parm[1]:'string';//输入类型
    $item['isRequire'] = isset($parm[2])?1:0;//是否必填参数
    $item['default'] = isset($parm[3])?$parm[3]:null;//默认值是多少，有默认值得情况
    $item['remark'] = isset($parm[4])?$parm[4]:null;//备注信息
     */
    public function flushData(){
        $markdom = new Markdom($this->data);
        return $markdom->flushWithTable($this->data,array(
            '参数名称','参数类型','是否必填','默认值','参数说明'
        ));
    }


    public function flush(){

        //分解data数据信息
        $this->stream = '<aside style="font-size: smaller;">';
        $this->stream .= '<h3>'.$this->title.'</h3>';
        $this->stream .= '<p style="font-size:0.75em;font-family: \'Courier New\', sans-serif;">'.
            '<span>作者：'.$this->author.'</span>'.
            '<span style="margin-left: 15px;">更新时间：'.$this->date.'</span>'.
            '</p>';
        $this->stream .= '<h4 style="border: 1px solid #333;padding: 10px 15px;border-radius: 5px;">目标地址:<span style="background-color: #333;color: #fff;padding: 5px 15px;margin-left: 10px;border-radius:5px;">'.$this->href.'</span></h4>';
        $this->stream .= '<p>请求方式:<span>'.$this->method.'</span></p>';
        $this->stream .= '<p>请求参数信息:</span></p>';
        $this->stream .= '<pre style="background: #f5f5f5;font-family: \'Courier New\', sans-serif;border: 1px solid #ccc;transition-property: color,opacity,border-color;overflow-x: auto;display: block;line-height: 1.5;position: relative;padding: 10px 15px!important;background-color: #333!important;margin: 15px 0;border-radius: 5px;word-wrap: break-word;color: #fff;">'.$this->flushData().'</pre>';
        $this->stream .= '<p>成功返回:</span></p>';
        $this->stream .= '<pre style="background: #f5f5f5;font-family: \'Courier New\', sans-serif;border: 1px solid #ccc;transition-property: color,opacity,border-color;overflow-x: auto;display: block;line-height: 1.5;position: relative;padding: 10px 15px!important;background-color: #333!important;margin: 15px 0;border-radius: 5px;word-wrap: break-word;color: #fff;">'.$this->ok.'</pre>';
        $this->stream .= '<p>失败返回:</span></p>';
        $this->stream .= '<pre style="background: #f5f5f5;font-family: \'Courier New\', sans-serif;border: 1px solid #ccc;transition-property: color,opacity,border-color;overflow-x: auto;display: block;line-height: 1.5;position: relative;padding: 10px 15px!important;background-color: #333!important;margin: 15px 0;border-radius: 5px;word-wrap: break-word;color: #fff;">'.$this->error.'</pre>';
        $this->stream .= '<p>备注说明:<span>'.$this->remark.'</span></p>';
        $this->stream .= '</aside>';
        return $this->stream;
    }

}